﻿using UnityEngine;

public class Player : MonoBehaviour
{
    public float speed;
    public float maxHP = 100f;
    public float currentHP;

    [SerializeField]
    private float hurtHP = 40f;

    private Rigidbody2D rb;
    private Vector2 movement;

    private void Start()
    {
        currentHP = maxHP;

        rb = GetComponent<Rigidbody2D>();
    }

    void Update()
    {
        Move();
    }

    private void FixedUpdate()
    {
        rb.MovePosition(rb.position + movement * speed);
    }

    private void OnCollisionExit2D(Collision2D collision)
    {
        Debug.Log($"OnCollisionExit2D: {collision.collider.gameObject.name}");
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.tag == "Enemy")
        {
            currentHP -= hurtHP;
            if (currentHP < 0)
            {
                currentHP = 0;
            }
            UIManager.Instance.PlayerHurt(hurtHP, currentHP, maxHP);
        }
    }

    void Move()
    {
        movement.x = Input.GetAxis("Horizontal");
        movement.y = Input.GetAxis("Vertical");
    }
}
